package com.threeigtache.threeigproject;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.threeigtache.utils.AndroidWorkaround;
import com.threeigtache.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class BasicActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE  = 100;
    private static final int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 101;
    private RelativeLayout mLeft;
    private RelativeLayout mRight;
    private TextView       mTitle;
    private LinearLayout   mContent_layout;
    private ProgressDialog mDialog;
    // 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
    private List<String> mPermissionList = new ArrayList<>();
    private View mHand;
    private View mBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //        入侵式状态栏，只有5.0以后才能用，并且只能加到R.layout.xxx前面
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);


        //专为华为手机设置的底部被遮挡
        if (AndroidWorkaround.checkDeviceHasNavigationBar(this)) {
            AndroidWorkaround.assistActivity(findViewById(android.R.id.content));
        }


        mLeft = findViewById(R.id.left);
        mRight = findViewById(R.id.right);
        mTitle = findViewById(R.id.title);
        mContent_layout = findViewById(R.id.content_layout);
        mHand = findViewById(R.id.hand);
        mBody = findViewById(R.id.body);
        mDialog = new ProgressDialog(BasicActivity.this);
        //设置进度条不可取消
        mDialog.setCancelable(false);
        mBody.setVisibility(View.GONE);

    }

    /**
     * 用来控制主界面的显示问题
     */
    public void showBody(){

        if (mHand==null){
            mHand = findViewById(R.id.hand);
        }
        if (mBody==null){
            mBody = findViewById(R.id.body);
        }
        mHand.setVisibility(View.GONE);
        mBody.setVisibility(View.VISIBLE);
    }

    //判断是否有网络
    public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }


    /**
     * 设置子类的布局，传一个layoutID;如：R.layout.activity_base
     * setContentView_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param layoutID void
     * @throws
     * @since 1.0.0
     */
    public void setContentView_V(int layoutID) {
        // 获得inflater
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 把继承该BaseAcitivyt的layoutID放进来 显示

        View view = inflater.inflate(layoutID, null);
        // content_layout.addView(view);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        // content_layout.addView(view, layoutParams);
        view.setLayoutParams(layoutParams);
        if (null != mContent_layout) {
            mContent_layout.addView(view);

        }
    }

    /**
     * 设置 Title的文字，传一个resID，如：R.string.home_title setTitle_V(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */

    public void setTitle_V(String msg) {
        mTitle.setText(msg);
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setLeftShow(int tag, int resID) {
        if (null == mLeft) {
            return;
        }
        mLeft.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mLeft.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            mLeft.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.selft, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            mLeft.addView(view, params);
        }
    }

    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setRightShow(int tag, int resID) {
        if (null == mRight) {
            return;
        }
        mRight.removeAllViews();
        mRight.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mRight.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            mRight.addView(view, params);
        }
    }

    /**
     * 获取 left , 左边按钮 getLift_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @return RelativeLayout
     * @throws
     * @since 1.0.0
     */
    public RelativeLayout getLift_V() {
        if (mLeft == null) {
            mLeft = findViewById(R.id.left);
            return mLeft;
        }
        return mLeft;
    }

    /**
     * 获取 right , 右边按钮 getRight_V(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @return RelativeLayout
     * @throws
     * @since 1.0.0
     */
    public RelativeLayout getRight_V() {
        if (mRight == null) {
            mRight = findViewById(R.id.right);
            return mRight;
        }
        return mRight;
    }

    //显示加载进度圈
    public void showProgrees(final String text) {
        Utils.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(text)) {
                    mDialog.setMessage("加载中,请稍等.....");
                } else {
                    mDialog.setMessage(text);
                }
                mDialog.show();
            }
        });
    }

    //隐藏加载进度圈
    public void hideProgree() {
        Utils.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                mDialog.dismiss();
            }
        });
    }

    //Android6.0权限申请
    public void getOnePermission(String permission) {
        //检查版本是否大于M
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{permission},
                        MY_PERMISSIONS_REQUEST_CALL_PHONE);

            } else {
                //TODO:权限申请成功
            }

        }
    }

    //Android 申请多个权限
    public void getAllPermission(String[] permissions) {
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(BasicActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }

        if (mPermissionList.isEmpty()) {

            //TODO:权限全部申请成功
        } else {
            //将List转为数组
            String[] permissiones = mPermissionList.toArray(new String[mPermissionList.size()]);
            ActivityCompat.requestPermissions(BasicActivity.this, permissiones, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //TODO:权限申请成功
            } else {
                //判断是否勾选禁止后不再询问
                boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(BasicActivity.this, permissions[0]);
                if (showRequestPermission) {
                    //TODO:权限申请成功
                } else {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    //        Uri uri = Uri.fromParts("package", context.getPackageName());
                    //        intent.setData(uri);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }

            }
        } else if (requestCode == MY_PERMISSIONS_REQUEST_CALL_CAMERA) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    //判断是否勾选禁止后不再询问
                    boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(BasicActivity.this, permissions[i]);
                    if (showRequestPermission) {
                        //TODO:权限全部申请成功
                    } else {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        //        Uri uri = Uri.fromParts("package", context.getPackageName());
                        //        intent.setData(uri);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                    }
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 这是专门做跳转界面的在父类BasicActivity中
     *
     * @param clazz   要跳转的界面.class
     * @param isFinsh 是否要让这个界面从任务栈中去除
     */
    public void skipOtherActivity(Class clazz, boolean isFinsh) {

        startActivity(new Intent(BasicActivity.this, clazz));

        if (isFinsh) {
            finish();
        }
    }
}
